package com.example.myapplication.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.BaseOnclickListener;
import com.example.myapplication.R;
import com.example.myapplication.entity.SearchResult;
import com.example.myapplication.entity.Singer;
import com.example.myapplication.entity.Song;

import java.util.ArrayList;
import java.util.List;

public class ResultSongAdapter extends RecyclerView.Adapter<ResultSongAdapter.ResultSongViewHolder> {
    private SearchResult result;
    private List<SearchResult.SongsBean.ArtistsBean> artistsBeanList;
    private SearchResult.SongsBean.AlbumBean albumBean;
    private BaseOnclickListener onclickListener;

    public void setData(SearchResult result){
        this.result = result;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ResultSongViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ResultSongViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_result_song, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ResultSongViewHolder holder, int position) {
        SearchResult.SongsBean songsBean = result.getSongs().get(position);
        artistsBeanList = result.getSongs().get(position).getArtists();
        SearchResult.SongsBean.ArtistsBean artistsBean = artistsBeanList.get(0);
        //albumBean = result.getSongs().get(position).getAlbum();

        holder.tvSongName.setText(songsBean.getName());
        holder.tvSingerName.setText(artistsBean.getName());
        //holder.tvAlbum.setText(artistsBean.getName());

        if (!songsBean.getMvid().equals("0")){
            holder.imgMV.setImageResource(R.drawable.mv);
        }

        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onclickListener != null){
                    onclickListener.click(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return result == null ? 0 : result.getSongs().size();
    }

    class ResultSongViewHolder extends RecyclerView.ViewHolder{
        TextView tvSongName;
        TextView tvSingerName;
        ImageButton imgMV;
        TextView tvAlbum;
        View view;

        public ResultSongViewHolder(@NonNull View itemView) {
            super(itemView);

            tvSongName = itemView.findViewById(R.id.tv_song_name);
            tvSingerName = itemView.findViewById(R.id.tv_singer_name);
            imgMV = itemView.findViewById(R.id.mv);
            tvAlbum = itemView.findViewById(R.id.tv_album);
            view = itemView.findViewById(R.id.view_layout);
        }
    }

    public void setBaseOnclickListener(BaseOnclickListener onclickListener){
        this.onclickListener = onclickListener;
    }
}
